package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.domain.Region;
import com.spzx.user.mapper.RegionMapper;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.service.RegionService;
import com.spzx.user.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2024-11-01
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private RegionService regionService;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询用户地址列表
     *
     * @return 用户地址
     */
    @Override
    public List<UserAddress> selectUserAddressList() {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //带条件的查询
        List<UserAddress> userAddressList = baseMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
        return userAddressList;
    }

    /**
     * 新增用户地址
     *
     * @param userAddress 用户地址
     * @return 结果
     */
    @Override
    public int inserUserAddress(UserAddress userAddress) {
        //获取用户id
        userAddress.setUserId(SecurityUtils.getUserId());
        //获取省市区的名字
        String provinceName = regionService.getNameByCode(userAddress.getProvinceCode());
        String cityName = regionService.getNameByCode(userAddress.getCityCode());
        String districtName = regionService.getNameByCode(userAddress.getDistrictCode());
        //拼接详细地址
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        //给userAddress设置详细地址
        userAddress.setFullAddress(fullAddress);

        //如果是默认地址，其他地址更新为非默认地址
        if (userAddress.getIsDefault().intValue() == 1) {
            UserAddress updateUserAddress = new UserAddress();
            updateUserAddress.setIsDefault(0L);
            baseMapper.update(updateUserAddress, new LambdaUpdateWrapper<UserAddress>().eq(UserAddress::getUserId, userAddress.getUserId()));
        }
        return baseMapper.insert(userAddress);
    }

    /**
     * 修改用户地址
     *
     * @param userAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        //获取省市区的名字
        String provinceName = regionService.getNameByCode(userAddress.getProvinceCode());
        String cityName = regionService.getNameByCode(userAddress.getCityCode());
        String districtName = regionService.getNameByCode(userAddress.getDistrictCode());
        //拼接详细地址
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        //给userAddress设置详细地址
        userAddress.setFullAddress(fullAddress);

        //如果是默认地址，其他地址更新为非默认地址
        if (userAddress.getIsDefault().intValue() == 1) {
            UserAddress updateUserAddress = new UserAddress();
            updateUserAddress.setIsDefault(0L);
            baseMapper.update(updateUserAddress, new LambdaUpdateWrapper<UserAddress>().eq(UserAddress::getUserId, userAddress.getUserId()));
        }
        return baseMapper.updateById(userAddress);
    }

    /**
     * 删除用户地址
     *
     * @param id 用户地址
     * @return 结果
     */
    @Override
    public int deleteNotDefaultAddressById(Long id) {
        //判断是否是默认的地址
        UserAddress userAddress = baseMapper.selectById(id);
        if(userAddress.getIsDefault() == 1){
            //抛出异常
            throw new ServiceException("该地址为默认地址，不能删除");
        }
        //根据id删除
        return baseMapper.deleteById(id);
    }

    //根据地区编号获取地区名字的方法
    public String getRegionNameByCode(String code) {
        //调用RegionMapper中带条件查询的方法
        Region region = regionMapper.selectOne(new LambdaQueryWrapper<Region>().eq(Region::getCode, code));
        //获取地区的名字
        return region.getName();
    }
}
